﻿using ApiManage.Models.zyhModels;
using ApiManage.Svc.Filter;
using Microsoft.AspNetCore.Mvc;
using Rhonin.Common.Entity.Page;
using Rhonin.Common.Extentions;
using System;
using System.Linq;
using System.Linq.Expressions;
using ApiManage.Service.MsSql.IService;
using ApiManage.Service.MsSql.Service;
using Rhonin.Common.Entity.Api;

namespace ApiManage.Svc.Controllers.System
{
    /// <summary>
    /// 通知
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class NoticeController : BaseController
    {
        private readonly ISysMessageService _sysMessageService = new SysMessageService();
        /// <summary>
        /// 分页获取消息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [Route("GetPage"), HttpGet, CheckLogin]
        public ApiResult<PageResult<SysMessage>> GetPage(int pageIndex, int pageSize)
        {
            QueryPage param = new QueryPage()
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            Expression<Func<SysMessage, bool>> where = o => o.Mark != 9;
            where = where.And(o => o.ReceiptType == 1);
            where = where.Or(o => o.ReceiptType == 2 && o.Recipient.Equals(UserId));
            var list = _sysMessageService.GetList(where).ToList();
            var result = new PageResult<SysMessage>()
            {
                TotalCount = list.Count,
                Items = list.OrderByDescending(o => o.IsTop).ThenByDescending(o => o.Createtime).Skip(param.Skip).Take(param.Limit),
                PageParameter = param
            };
            return CreateResult(result);
        }
    }
}